import Toast from '../../@vant/weapp/toast/toast';
import {IMG_PATH} from '../../config/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName:['1','2','3','4','5','6'],
    qualificationList:[],
    searchTimeFlag:false,
    selectTimeId:'',
    selectDate:'',
    minDate: new Date(2010, 10, 1).getTime(),
    searchTimeFormatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  collapseChange(event){
    this.setData({
      activeName: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let qualificationList = [{
      id:1,
      time:'',
      qualificationName:'',
      level:'',
      qualificationCode:'',
      office:''
    }];
    this.setData({
      qualificationList:qualificationList
    })
  },
  onInput(event) {
    this.setData({
      selectDate: event.detail,
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  searchTimeConfirm(event){
    let self = this;
    console.log(event.detail);
    let dataList = self.data.qualificationList;
    dataList.forEach((item)=>{
      if(item.id == self.data.selectTimeId){
        item.time = `${self.formatDate(event.detail)}`;
      }
    })
    self.setData({
      qualificationList:dataList,
      searchTimeFlag:false,
    })
  },
  officeChange(event){
    let id = event.currentTarget.dataset.id;
    let dataList = this.data.qualificationList;
    dataList.forEach((item)=>{
      if(item.id == id){
        item.office = event.detail;
      }
    })
    this.setData({
      qualificationList:dataList
    })
  },
  qualificationCodeChange(event){
    let id = event.currentTarget.dataset.id;
    let dataList = this.data.qualificationList;
    dataList.forEach((item)=>{
      if(item.id == id){
        item.qualificationCode = event.detail;
      }
    })
    this.setData({
      qualificationList:dataList
    })
  },
  levelChange(event){
    let id = event.currentTarget.dataset.id;
    let dataList = this.data.qualificationList;
    dataList.forEach((item)=>{
      if(item.id == id){
        item.level = event.detail;
      }
    })
    this.setData({
      qualificationList:dataList
    })
  },
  qualificationNameChange(event){
    console.log(event)
    let id = event.currentTarget.dataset.id;
    let dataList = this.data.qualificationList;
    dataList.forEach((item)=>{
      if(item.id == id){
        item.qualificationName = event.detail;
      }
    })
    this.setData({
      qualificationList:dataList
    })
  },
  searchTimeCancel(){
    this.setData({
      searchTimeFlag:false
    })
  },
  searchTimeClick(event){
    console.log(event)
    let id = event.currentTarget.dataset.id;
    this.setData({
      searchTimeFlag:true,
      selectTimeId:id
    })
  },
  // 新增资格证书
  addQualification(event){
    console.log(event);
    let currentObj = event.currentTarget.dataset.obj;
    let dataList = this.data.qualificationList;
    if(currentObj.time && currentObj.qualificationName && currentObj.level && currentObj.qualificationCode && currentObj.office){
      dataList.push({
        id:`${currentObj.id}+1`,
        time:'',
        qualificationName:'',
        level:'',
        qualificationCode:'',
        office:''
      })
      this.setData({
        qualificationList:dataList
      })
    }else{
      Toast.fail('请把当前数据填完再新增！');
    }
    
    
  },
  // 删除资格证书
  deleteQualification(event){
      console.log(event)
      let currentObj = event.currentTarget.dataset.obj;
      let dataList = this.data.qualificationList;
      if(dataList && dataList.length && dataList.length>=2){
        dataList.forEach((item,index)=>{
          if(item.id == currentObj.id){
            dataList.splice(index,1)
          }
        })
        this.setData({
          qualificationList:dataList
        })
      }else{
        Toast.fail('最后一个资格证书无法删除！');
      }
      
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})